package ro.dandanciu.webgallery.ui.general;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.SessionAware;

import ro.dandanciu.webgallery.domainmodel.Gallery;
import ro.dandanciu.webgallery.domainmodel.Roles;
import ro.dandanciu.webgallery.domainmodel.User;
import ro.dandanciu.webgallery.services.GalleryServices;

import com.opensymphony.xwork2.ActionSupport;

/**
 * Base Action class for the Tutorial package.
 */
@SuppressWarnings("serial")
public class WebGalleryAction extends ActionSupport implements ServletRequestAware, SessionAware{

	protected static final String MESSAGE = "message";
	
	protected Map<String, Object> session = new HashMap<String, Object>();

	private HttpServletRequest request;
	
	protected Set<Roles> requiredRoles = new TreeSet<Roles>();
	
	protected GalleryServices galleryServices = null;

	@SuppressWarnings("unchecked")
	
	public void setSession(Map session) {
		this.session = session;
	}
	
	protected User getSessionUser() {
		if (session == null) {
			return null;
		}
		return (User) session.get(Constants.SESSION_USER_KEY);
	}
	
	protected void setSessionUser(User user) {
		if (session != null) {
			session.put(Constants.SESSION_USER_KEY, user);
		}
	}
	
	protected void removeSessionUser() {
		if (session.containsKey(Constants.SESSION_USER_KEY)) {
			session.remove(Constants.SESSION_USER_KEY);
		}
	}
	
	protected boolean isSessionValid() {
		return (session != null) && (getSessionUser() != null);
	}
	
	protected String setMessage(String key) {
		request.setAttribute("message.key", key);
		return MESSAGE;
	}

	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
		
	}

	public Set<Roles> getRequiredRoles() {
		return requiredRoles;
	}

	public void setRequiredRoles(Set<Roles> requiredRoles) {
		this.requiredRoles = requiredRoles;
	}
	
	public List<Gallery> getPublicGalleries() {
		return galleryServices.getPublicGalleries("");
	}

	public GalleryServices getGalleryServices() {
		return galleryServices;
	}

	public void setGalleryServices(GalleryServices galleryServices) {
		this.galleryServices = galleryServices;
	}
}
